package com.example.demo;

import java.io.FileOutputStream;

import org.xhtmlrenderer.pdf.ITextRenderer;

import com.lowagie.text.DocumentException;

public class HtmlToPdfUsingITextRenderer {
    public static void convertHtmlToPdf(String inputHtml, String outputPdfPath) {
        try {
			ITextRenderer renderer = new ITextRenderer();
			renderer.setDocumentFromString(inputHtml); // 如果是字符串形式的HTML内容
			// renderer.setDocument(new File(inputHtml)); // 如果是本地HTML文件

			renderer.layout();

			try (FileOutputStream os = new FileOutputStream(outputPdfPath)) {
			    try {
					renderer.createPDF(os);
				} catch (DocumentException e) {
					e.printStackTrace();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
}